package com.ning.parking.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingLotBaseInfo;
import com.ning.parking.domain.ParkingLotMonthlyApply;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingLotMonthlyApplyCarDTO;
import com.ning.parking.domain.dto.ParkingLotMonthlyApplyPageDto;
import com.ning.parking.domain.dto.ParkingLotMonthlyApplyProveDTO;
import com.ning.parking.service.ParkingLotBaseInfoService;
import com.ning.parking.service.ParkingLotMonthlyApplyService;
import com.ning.parking.service.ParkingRecordService;
import com.scsonic.framework.common.response.R;
import com.scsonic.framework.exception.BizException;
import com.scsonic.framework.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChenN
 * @Date 2025/4/8 15:41
 * @Version 0.0.1
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("monthly")
public class ParkingLotMonthlyController {

    final ParkingLotMonthlyApplyService parkingLotMonthlyApplyService;
    final ParkingRecordService parkingRecordService;
    final ParkingLotBaseInfoService parkingLotBaseInfoService;

    /**
     * ------------------------------
     * @param applyEntity
     * @param applyCarList
     * @param baseProveList
     * @return
     */
    @PostMapping("saveApplyInfo")
    public R<Long> saveApplyInfo(ParkingLotMonthlyApplyPageDto applyEntity, List<ParkingLotMonthlyApplyCarDTO> applyCarList, List<ParkingLotMonthlyApplyProveDTO> baseProveList){
        Long aLong = parkingLotMonthlyApplyService.saveApplyInfo(applyEntity, applyCarList, baseProveList);
        return R.ok(aLong);
    }

    @PostMapping("save-monthly")
    public R<Long> saveMonthly(@RequestBody ParkingLotMonthlyApply parkingLotMonthlyApply) {
        DateTime date = DateUtils.date();
        parkingLotMonthlyApply.setUpdateTime(date);
        parkingLotMonthlyApply.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotMonthlyApply.setCreateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotMonthlyApply.setCreateTime(date);

        if (parkingLotMonthlyApply.getMonthlyStartDay() == null) {
            throw new BizException("MonthlyStartDay is null");
        }

        if (parkingLotMonthlyApply.getMonthlyEndDay() == null) {
            throw new BizException("MonthlyEndDay is null");
        }

        ParkingLotMonthlyApply parkingLotMonthlyS = parkingLotMonthlyApplyService.selectMonthlyExit(parkingLotMonthlyApply.getCarLicense(), parkingLotMonthlyApply.getMonthlyStartDay(), parkingLotMonthlyApply.getParkingLotId());

        if (parkingLotMonthlyS != null){
            throw new BizException(DateUtils.format(parkingLotMonthlyApply.getMonthlyStartDay(),DateUtils.YYYY_MM_DD_HH_MM_SS)+"存在月租");
        }

        parkingLotMonthlyS = parkingLotMonthlyApplyService.selectMonthlyExit(parkingLotMonthlyApply.getCarLicense(), parkingLotMonthlyApply.getMonthlyEndDay(), parkingLotMonthlyApply.getParkingLotId());

        if (parkingLotMonthlyS != null){
            throw new BizException(DateUtils.format(parkingLotMonthlyApply.getMonthlyEndDay(),DateUtils.YYYY_MM_DD_HH_MM_SS)+"存在月租");
        }

        parkingLotMonthlyApplyService.saveOrUpdate(parkingLotMonthlyApply);
        parkingRecordService.updateCarTypeInfo(parkingLotMonthlyApply);
        return R.ok(parkingLotMonthlyApply.getMonthlyId());
    }

    @PostMapping("upd-monthly")
    public R<Long> updWhitelist(@RequestBody ParkingLotMonthlyApply parkingLotMonthlyApply) {
        if (parkingLotMonthlyApply.getMonthlyId() == null) {
            throw new BizException("MonthlyId is null ");
        }
        DateTime date = DateUtils.date();
        parkingLotMonthlyApply.setUpdateTime(date);
        parkingLotMonthlyApply.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        parkingLotMonthlyApplyService.updateById(parkingLotMonthlyApply);
        return R.ok(parkingLotMonthlyApply.getMonthlyId());
    }

    @GetMapping("get-by-monthly-id")
    public R<ParkingLotMonthlyApply> getByWhitelistId(ParkingLotMonthlyApply parkingLotMonthlyApply) {
        return R.ok(parkingLotMonthlyApplyService.getById(parkingLotMonthlyApply.getMonthlyId()));
    }

    @GetMapping("page")
    public R<Pagination<ParkingLotMonthlyApply>> page(ParkingLotMonthlyApplyPageDto parkingLotMonthlyApply) {
        String carLicense = parkingLotMonthlyApply.getCarLicense();
        if (ObjectUtil.isEmpty(carLicense)){
            parkingLotMonthlyApply.setCarLicense(null);
        }
        Pagination<ParkingLotMonthlyApply> page = parkingLotMonthlyApplyService.selectPage(parkingLotMonthlyApply);
        List<ParkingLotMonthlyApply> records = page.getRecords();
        for (ParkingLotMonthlyApply record : records) {
            if (record.getParkingLotId()!=null){
                ParkingLotBaseInfo byId = parkingLotBaseInfoService.getById(record.getParkingLotId());
                record.setParkingLotName(byId!=null?byId.getParkingLotName():"--");
                continue;
            }
            record.setParkingLotName("--");
        }
        return R.ok(page);
    }
}
